package com.kyzh.core.dialog

import android.app.Activity
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.kyzh.core.R
import com.kyzh.core.utils.dip


/*ListPopupwindow*/
fun Activity.shouListWindow(arrayList: ArrayList<String>, listener: Int.() -> Unit): PopupWindow {
    val view: View = LayoutInflater.from(this).inflate(R.layout.dialog_list_pop, null)
    val window = PopupWindow(view, ViewGroup.MarginLayoutParams.WRAP_CONTENT, dip(48) * arrayList.size)

    val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)


    val adapter  = Adapter(android.R.layout.simple_list_item_1, arrayList)
    window.contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
    recyclerView.adapter = adapter
    adapter.setOnItemClickListener { adapter, view, position ->
        position.listener()
        window.dismiss()
    }
    window.animationStyle = R.style.PopupwindowAnim
    window.elevation = 5f
    window.isOutsideTouchable = true
    window.isFocusable = true
    return window
}

private class Adapter(layout: Int, beans: ArrayList<String>) :
        BaseQuickAdapter<String, BaseViewHolder>(layout, beans) {

    override fun convert(holder: BaseViewHolder, item: String) {
        holder.setText(android.R.id.text1, item)
                .setTextColor(
                        android.R.id.text1,
                        ContextCompat.getColor(context,R.color.font_33)
                )
        val text = holder.getView<TextView>(android.R.id.text1)
        text.gravity = Gravity.CENTER

    }
}
